<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
</body>
<script>
    // 发布者
    class Dep {
        constructor() {
            // 记录所有的订阅者
            this.subs = [];
        }

        addSub(sub) {
            if (sub && sub.update) {
                this.subs.push(sub);
            }
        }

        notify() {
            this.subs.forEach(sub => {
                sub.update();
            })
        }
    }

    // 订阅者
    class Watcher {
        update() {
            console.log('update');
        }
    }

    // test
    let dep = new Dep();
    let watcher = new Watcher();

    dep.addSub(watcher);

    dep.notify();
</script>
</html>